{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aaef6eb0",
   "metadata": {},
   "source": [
    "# 函数\n",
    "\n",
    "## 函数的定义\n",
    "> 1.数学：    f（x）= ax+b  \n",
    "> 2.python: def fib(x)  \n",
    "> 3.编程思维和数学思维的区别  \n",
    ">> 1.函数可以被定义名称，定义的关键字 <font style=\"color:red\">**def**</font>  \n",
    ">> 2.数学思维是卸载同一行（定义函数  和运算公式）），面向对象编程种  函数写多行，定义函数为单独一行  \n",
    ">> 3.数学函数一没有注释，\n",
    ">> 4.数学中 ： ax+b  是计算公式，而编程中，我们将函数的内容成为**函数体**，函数体要有一定的**缩进**  \n",
    ">> 5.数学函数执行，将参数带入公式，编程中函数执行，将参数带入函数体\n",
    "\n",
    "## [关于函数的更多定义](https://docs.python.org/3/tutorial/contro)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23b17e02",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(x):\n",
    "    '''列出指定x以内的斐波那契数列'''\n",
    "    a,b = 0,1\n",
    "    while a < x :\n",
    "        print(a,end=\" \")\n",
    "        a,b = b, a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2247508",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 "
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "04426f53",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请大家写一个函数，这个函数可以打印“你好，XXX同学”\n",
    "def my_function():\n",
    "    '''你好，铠甲勇士\n",
    "    \n",
    "    '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b287653b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，铠甲勇士\n",
      "    \n",
      "    \n"
     ]
    }
   ],
   "source": [
    "print(my_function.__doc__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c694deca",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请大家写一个函数，这个函数可以打印“你好，XXX同学”\n",
    "def hello(name):\n",
    "    '''显示问候语'''\n",
    "    print(\"你好，%s同学\"%(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bfda5907",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，流氓同学\n"
     ]
    }
   ],
   "source": [
    "hello(\"流氓\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fab3b2f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e2fed43b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sleep()需要给定一个时间（int）参数\n",
    "time.sleep(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2ed9ada6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=2, tm_hour=10, tm_min=54, tm_sec=35, tm_wday=3, tm_yday=336, tm_isdst=0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# location()不需要给定参数\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d23b978",
   "metadata": {},
   "source": [
    "> 3.函数的优势  \n",
    ">> 1.可以重复使用相同的功能，避免重复造轮子  \n",
    ">> 2.使用者可以不了解函数本身，只需要清楚函数使用即可  \n",
    ">> 3.使用/调用方便，只需要清楚传递的函数以及参数  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ac69ba0",
   "metadata": {},
   "source": [
    "## 函数的参数\n",
    "> 1.形式与参数  \n",
    ">> 在**定义**函数的时候定义的参数  \n",
    ">> 形式参数不占用内存  \n",
    "\n",
    "> 2.实际参数  \n",
    ">> 在**调用**函数的时候传递的参数  \n",
    ">> 调用函数时，形式参数分配空间，传递实际参数，执行函数内容  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e34b68af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课本函数实践案例\n",
    "def search4vowel(word):\n",
    "    '''显示文本中出现的元音有哪些'''\n",
    "    vowel = set(\"seiou\")\n",
    "    word = set(word)\n",
    "    print(vowel & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "db707984",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'s', 'e', 'o', 'u', 'i'}\n"
     ]
    }
   ],
   "source": [
    "search4vowel(\"oceancanyouspeakenglish\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a2b3017",
   "metadata": {},
   "outputs": [],
   "source": [
    "* 在多个参数\n",
    "\n",
    "> 1.位置参数（positional arguement）\n",
    ">> 1.在形式参数的定义中，有一定的先后顺序\n",
    ">> 2.在调用函数时，要考虑形参的先后顺序\n",
    ">> 3.调用函数时，不仅要考虑参数位置，还需要考虑形式参数的个数\n",
    "> 2.关键参数 \n",
    "\n",
    "> 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "41129a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(pet_name,anlmal_type):\n",
    "    '''显示宠物信息'''\n",
    "    print(\"\\nI have a \" + anlmal_type + \".\")\n",
    "    print(\"My\" + anlmal_type + \"'s name is'\"+pet_name.title() + '.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "1f038c89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is'流氓.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"流氓\",\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6b19cedb",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 1 required positional argument: 'anlmal_type'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-39-01b098cd7e74>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"江老板\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 1 required positional argument: 'anlmal_type'"
     ]
    }
   ],
   "source": [
    "describe_pet(\"江老板\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f7e415f",
   "metadata": {},
   "source": [
    "* 参数注意事项实践\n",
    "> 1.位置参数和关键字\n",
    ">> 1.positional arguement follows keyword arguement（关键字参数<font style=\"color:red\">均</font>应该放在位置参数之后）\n",
    ">> 2.只要满足1的条件，剩余的关键字参数（多个）不考虑先后顺序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "05a6a7b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(x,y,z):\n",
    "    '''打印xyz的结果'''\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4e742b82",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-42-489fdb94d38c>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-42-489fdb94d38c>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    test(5,y=3,2)\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "# 实践1：\n",
    "test(5,y=3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "897b0fc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 实践2：\n",
    "test(5,y=3,z = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "62566371",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 实践3：\n",
    "test(5,z=3,y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "861063dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def test(x,y,z):\n",
    "    '''打印xyz的结果'''\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
